//
// Created by HP on 2022/3/8.
//

// std
#include <assert.h>
#include <stdio.h>
#include <string.h>

#define STB_IMAGE_WRITE_IMPLEMENTATION
#include "include/stb_image_write.h"

#include "video.h"

void initScreen() {
    memset(screen, ' ', HEIGHT * WIDTH);
    for (int i = 0; i < HEIGHT; i++) {
        screen[i][WIDTH - 1] = 0;
    }
}

void drawPixel(int x, int y) {
    assert(x < WIDTH && y < HEIGHT);
    screen[y][x] = PIXEL;
}

void drawDot(int x, int y) {
    assert(x < WIDTH && y < HEIGHT);
    screen[y][x] = DOT;
}

void clearScreen() {
    for (int i = 0; i <= HEIGHT; i++) {
        printf("\033[1A\033[1A\n");;
    }
}

void showScreen(int enable, const char *name) {
    if (enable) {
        // 抗锯齿
        for (int y = 0; y < HEIGHT; y++) {
            for (int x = 0; x < WIDTH; x++) {
                unsigned char up = 0;
                unsigned char down = 0;
                unsigned char left = 0;
                unsigned char right = 0;
                if (y < HEIGHT - 1) {
                    up = screen[y + 1][x];
                }
                if (y > 0) {
                    down = screen[y - 1][x];
                }
                if (x > 0) {
                    left = screen[y][x - 1];
                }
                if (x < WIDTH - 1) {
                    right = screen[y][x + 1];
                }
                screen[y][x] = (up + down + left + right) / 4;
            }
        }
    }
    stbi_write_bmp(name, WIDTH, HEIGHT, 1, screen);
}


